package com.orange.cepmobile.android;

import android.app.Application;
import android.content.Context;
import android.os.StrictMode;

import com.orange.cepmobile.android.tools.GpsLocation;
import com.orange.cepmobile.android.tools.ImageLoader;

public class CEPMobileApplication extends Application {
	public static final String TAG = CEPMobileCommon.CEPMOBILE_TAG;
	private ImageLoader mImageLoader = null;
	public static final String PREFS = "";
	private static CEPMobileApplication instance = null;
	private static Context context = null;
	private Boolean mReachable = null;
	private final static String SERVER_URL = "";

	public static CEPMobileApplication getInstance() {
		if (instance == null) {
			throw new IllegalStateException("Application not created yet!");
		}

		return instance;
	}

	@Override
	public void onCreate() {
		instance = this;
		context = getApplicationContext();
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}

		GpsLocation.initLocationManager();
		// applyPreferences();
	}

	public static Context getAppContext() {
		return context;
	}

	/*
	 * web-services
	 */
	// public void applyPreferences() {
	// SharedPreferences prefs = PreferenceManager
	// .getDefaultSharedPreferences(getApplicationContext());
	//
	// // Set base-url from preferences
	// String baseUrl = prefs.getString("dev_environment",
	// "http://"+SERVER_URL);
	//
	// // Set default value if its empty (default values in preferences.xml are
	// // not returned here).
	// if (!prefs.contains("dev_environment")) {
	// prefs.edit().putString("dev_environment", baseUrl).commit();
	// }
	//
	// try {
	// CEPMobileREST.setBaseUrl(baseUrl);
	// } catch (URISyntaxException e) {
	// Log.e(TAG, "Could not set base-URL: " + baseUrl);
	// }
	// }
	/**
	 * Public accessor for image-loader, so all activities all use the same
	 * instance.
	 * 
	 * @return ImageLoader
	 */
	public ImageLoader getImageLoader() {
		if (mImageLoader == null) {
			mImageLoader = new ImageLoader(this);
		}

		return mImageLoader;
	}
}
